Chain.js
These two functions allow you to easily modify the behavior of any mutable methods while (optionally) preserving the old logic. Your logic may filter the method's input and/or output, and/or decide whether the old logic should be executed at all. setCallbacks var failed = setCallbacks(object, callbacks) Wraps specified methods on an object in callbacks. Returns a numeric array of method names which don't exist on object, or false to indicate an invalid argument. object - must be non-null, and have a typeof value of "object" or "function". Boolean, Number, and String objects may work. callbacks - an object of functions. Each key should be the corresponding function's name on object, and each value must be either: * a function, or * an object with properties 'func' or '0', the function, and 'when' or '1', the corresponding when value (see documentation for chain() below). chain var success = chain(object, funcName, callback, = 1) Wraps an original method in a user-supplied function. All state is transferred to the new function and may be modified as desired to affect the original, the caller, or both, or to discard the original completely. If funcName does not indicate a method of object or the arguments are otherwise invalid, false is returned; otherwise, true is returned. object - must be non-null, and have a typeof value of "object" or "function". Boolean, Number, and String objects may work. Defaults to the global object (i.e. window) according to the ECMAScript specification. funcName - the name used by the object to refer to the target function - NOT the actual function. callback - your function. In all cases, this will be the passed object in both functions. when - optional; determines when your function will be executed in relation to the original; defaults to 1 for all unrecognized values. Values are interpreted as follows: -1 - before. Your function will be passed the arguments meant for the original. If your function returns an array, the contents will be passed to the original as the ordinary arguments. 0 - around. Your function will be passed the original function and the arguments, and will be responsible for calling the original. Your return value will be seen as the original's to outside code. (See Function.call and Function.apply.) 1 - after. Your function will be passed the original's return value and arguments. If you return a value (other than undefined), it will override the original's. Source code (license) function chain(object, funcName, neu, when){ if((typeof object != "object" && typeof object != "function") || object null) return false; if(typeof funcName != 'string' || typeof neu != 'function') return false; if(typeof when != 'number' || when < -1 || when > 1 || when % 1 != 0) when = 1; oldFunc = objectfuncName; if(typeof oldFunc != 'function') return false; objectfuncName = function(){ var result1, result2, args = []; for(var i = 0; i < arguments.length; i++) argsi = argumentsi; if(when 1){ result1 = oldFunc.apply(object, args); args.unshift(result1); } if(when 0) args.unshift(oldFunc); result2 = neu.apply(object, args); if(when -1){ if(typeof result2 'object' && result2.constructor Array) args = result2; return oldFunc.apply(object, args); } if(typeof result2 != 'undefined') return result2; else return result1; }; return true; } function setCallbacks(object, callbacks){ if((typeof object != "object" && typeof object != "function") || object null) return false; if((typeof callbacks != "array" && typeof callbacks != "object") || callbacks null) return false; var results = []; if(typeof callbacks != 'undefined'){ for(i in callbacks){ var neu = callbacksi, when; if(typeof neu 'object' && neu.constructor != Function){ when = neu.when || neu1; neu = neu.func || neu0 || neu; } if(!chain(object, i, neu, when)) results.push(i); } } return results; } Licenses Documentation Source code Category:Wiki scripts